package startsql;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;


import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;





public class StartSQL {

	
	static ArrayList<ConnectSet> connectsList = new ArrayList<>();
	static ArrayList<CommandSet> queryList = new ArrayList<>();
	
	/**
	 * 
	 * @param args
	 */
	@SuppressWarnings("static-access")
	public static void main(String args[]) {

		

		 
		final ArrayList<ConnectDBFabric> conectBaseTest = new ArrayList<>();

		
		final MainFrame frameMain = new MainFrame();
		final CrateConnectFrame frameCreate = new CrateConnectFrame();

		frameMain.setVisible(true);
		

		
		frameMain.butSQL.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				
				int numIndex = MainFrame.getIndexEl();

				try {
					if(numIndex >= 0){
						conectBaseTest.get(numIndex).Query(frameMain.getInputText());
						frameMain.RefrashOut(conectBaseTest.get(numIndex).GetView());
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		
		frameMain.butAddEl.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				frameCreate.butAdd.setEnabled(true);
				frameCreate.setVisible(true);
				frameCreate.butEditConnect.setEnabled(false);
				
				frameMain.butLoad.setEnabled(false);
			}
		});
		
		
		frameCreate.butAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				
				String name = frameCreate.getNameConnect();
				int dataBase = frameCreate.getDataBase();
				String ip = frameCreate.getIP();
				String base = frameCreate.getBase();
				String user = frameCreate.getUser();
				String pass = frameCreate.getPass();
				
				frameMain.createNewEl(name);
				
				
				int numIndex = MainFrame.getCountEl();
				
				if(numIndex >= 0 && name != null){
					connectsList.add(new ConnectSet(name, dataBase, ip, base, user, pass));
					conectBaseTest.add(new ConnectDBFabric());
					conectBaseTest.get(numIndex-1).Initialization(connectsList.get(connectsList.size()-1));
					conectBaseTest.get(numIndex-1).CreateConnectDatabase(numIndex-1);
				}
				
				frameCreate.setVisible(false);
			}
		});
		
		frameCreate.butEditConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				
				int numIndex = MainFrame.getIndexEl();
				

				String name = frameCreate.getNameConnect();
				int dataBase = frameCreate.getDataBase();
				String ip = frameCreate.getIP();
				String base = frameCreate.getBase();
				String user = frameCreate.getUser();
				String pass = frameCreate.getPass();
				
				
				if(numIndex >= 0 && name != null){
					connectsList.set(numIndex,new ConnectSet(name, dataBase, ip, base, user, pass));
					//conectBaseTest.add(new BaseConnectFabric());
					conectBaseTest.get(numIndex).Initialization(connectsList.get(numIndex));
					conectBaseTest.get(numIndex).CreateConnectDatabase(numIndex);
				}
				
				frameCreate.setVisible(false);
			}
		});
		
		frameCreate.butTest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				
				/*String IP = frameCreate.getIP();
				String BASE = frameCreate.getBase();
				String USER = frameCreate.getUser();
				String PASS = frameCreate.getPass();*/
				frameCreate.setConnectTest();
			}
		});
		
		frameCreate.butClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				frameMain.setEnabled(true);
				frameCreate.setVisible(false);
			}
		});
		
		frameMain.butEditEl.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				
				
			 
				int numIndex = MainFrame.getIndexEl();
			
			if(numIndex >= 0){	

				String[] dataIn = connectsList.get(numIndex).getDataSet();
				
				frameCreate.setName(dataIn[0]);
				frameCreate.setDataBase(Integer.parseInt(dataIn[1]));
				frameCreate.setIP(dataIn[2]);
				frameCreate.setBase(dataIn[3]);
				frameCreate.setUser(dataIn[4]);
				frameCreate.setPass(dataIn[5]);
				
				
				frameCreate.butEditConnect.setEnabled(true);
				frameCreate.setVisible(true);
				frameCreate.butAdd.setEnabled(false);
			 }
			}
		});
		
		frameMain.butAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				String strName = MainFrame.getNameFile();
				queryList.add(new CommandSet(strName,frameMain.getInputText()));
				frameMain.createNewElCommand(strName);
				
				frameMain.butLoad.setEnabled(false);
			}
		});
		

		
		frameMain.LlistCommand.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				// TODO Auto-generated method stub
				if (!e.getValueIsAdjusting()){
					if(frameMain.getNameElCommand()!=null && frameMain.getIndexElCommand()>=0){
					String[] tmp = queryList.get(frameMain.getIndexElCommand()).getDataSet();
					frameMain.setInputText(tmp[1]);
					frameMain.setNameCommandCurrent(frameMain.getNameElCommand());
					}
				}

			}
		});
		
		frameMain.butEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				if(frameMain.getIndexElCommand()>=0)
				queryList.set(frameMain.getIndexElCommand(),new CommandSet(frameMain.getNameElCommand(), frameMain.getInputText()));
				
			}
		});
		
		frameMain.butSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				XMLGetSet.SetDataXML(connectsList);
				XMLGetSet.SetDataXMLString(queryList);
			}
		});

		
		frameMain.butConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				
				
					int numIndex = MainFrame.getIndexEl();
					
					if(numIndex >= 0){
					conectBaseTest.get(numIndex).Initialization(connectsList.get(numIndex));
					conectBaseTest.get(numIndex).CreateConnectDatabase(numIndex);
					}
			}
		});
		
		frameMain.butDisConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {

			}
		});
		
		
		frameMain.butLoad.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				
				connectsList = XMLGetSet.GetDataXML();
				queryList = XMLGetSet.GetDataXMLCommand();
				
				for(int i=0; i<connectsList.size(); i++){
					String[] elem = connectsList.get(i).getDataSet();
						String name = elem[0];
						/*int dataBase = Integer.parseInt(elem[1]);
						String ip = elem[2];
						String base = elem[3];
						String user = elem[4];
						String pass = elem[5];*/
						
						frameMain.createNewEl(name);
						conectBaseTest.add(new ConnectDBFabric());
				}
				
				for(int i=0; i<queryList.size(); i++){
					String[] elem = queryList.get(i).getDataSet();
						String name = elem[0];
						//String SQL = elem[1];
						
						frameMain.createNewElCommand(name);
				}
				
				
			}
		});
		
		frameMain.TextIn.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				//System.out.println("1");
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				//System.out.println("2");
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				String a = frameMain.getInputText();
				//System.out.println(a);
				
				
				String [] numbers = a.split(" ");
				
				
				int aaa = 0;
				for(String aa: numbers) {
				     if(aa.equals("select")){
						SimpleAttributeSet attr = new SimpleAttributeSet();
						StyleConstants.setForeground(attr, Color.red);
						frameMain.TextIn.getStyledDocument().setCharacterAttributes(aaa, aa.length(), attr, true);
						
				     }
				     aaa +=aa.length()+1;
				}
			}
		});
			

		
	}
	
	
	
	
	
	/*static void removeDataSet(){
		dataS.remove(dataS.size()-1);
	}*/
	
	
}
